package com.vipboot.platform.system.controller.rbac;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vipboot.framework.core.domain.R;
import com.vipboot.framework.core.validation.AddGroup;
import com.vipboot.framework.core.validation.EditGroup;
import com.vipboot.framework.log.annotation.Log;
import com.vipboot.framework.log.enums.BusinessType;
import com.vipboot.framework.mybatisplus.core.page.PageData;
import com.vipboot.framework.mybatisplus.core.page.PageQuery;
import com.vipboot.platform.system.crud.service.rbac.SysClientService;
import com.vipboot.platform.system.domain.bo.SysClientBo;
import com.vipboot.platform.system.domain.vo.SysClientVo;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统授权表 前端控制器
 * </p>
 *
 * @author daze
 * @since 2024-12-10
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysClient")
public class SysClientController {
    private final SysClientService sysClientService;

    /**
     * 分页查询系统授权表
     */
    @SaCheckPermission("sysClient:list")
    @GetMapping("/page")
    public R<PageData<SysClientVo>> page(@ModelAttribute SysClientBo bo,
                                         @ModelAttribute PageQuery pageQuery) {
        return R.ok(sysClientService.page(bo, pageQuery));
    }

    /**
     * 添加系统授权表
     */
    @SaCheckPermission("sysClient:save")
    @Log(module = "客户端管理", title = "添加客户端", businessType = BusinessType.Insert)
    @PostMapping("/save")
    public R<Void> save(@Validated(AddGroup.class)
                        @RequestBody SysClientBo bo) {
        return sysClientService.save(bo) ? R.ok() : R.fail();
    }


    /**
     * 根据主键更新系统授权表。
     */
    @SaCheckPermission("sysClient:update")
    @Log(module = "客户端管理", title = "更新客户端", businessType = BusinessType.Update)
    @PostMapping("/update")
    public R<Void> update(@Validated(EditGroup.class)
                          @RequestBody SysClientBo bo) {
        return sysClientService.update(bo) ? R.ok() : R.fail();
    }


    /**
     * 根据主键删除系统授权表。
     */
    @SaCheckPermission("sysClient:remove")
    @Log(module = "客户端管理", title = "删除客户端", businessType = BusinessType.Delete)
    @PostMapping("/remove/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return sysClientService.removeById(id) ? R.ok() : R.fail();
    }

    /**
     * 更新系统授权表状态
     */
    @SaCheckPermission("sysClient:update")
    @Log(module = "客户端管理", title = "更新客户端状态", businessType = BusinessType.Update)
    @PostMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysClientBo bo) {
        return sysClientService.changeStatus(bo.getClientId(), bo.getStatus()) ? R.ok() : R.fail();
    }

}
